package com.ja.subclass.service.impl;

import com.ja.enumall.StatusEnum;
import com.ja.subclass.mapper.SubMapper;
import com.ja.subclass.pojo.SubClass;
import com.ja.subclass.pojo.SubClassSelect;
import com.ja.subclass.pojo.SubClassVo;
import com.ja.subclass.service.SubService;
import com.ja.util.AllRecords;
import com.ja.util.ResultBuildVo;
import com.ja.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 11936 on 2019/4/25.
 */

@Service
public class SubServiceImpl implements SubService {
    @Autowired
    private SubMapper subMapper;

    @Override
    @Transactional
    public ResultVo addSubClass(SubClassVo subClassVo) {
        List<SubClass> subClasses = subMapper.queryByCode(subClassVo);
        if(!subClasses.isEmpty()){
            return ResultBuildVo.error("备件小类编码不能重复","550");
        }
        subMapper.addSubClass(subClassVo);
        return ResultBuildVo.success(subClassVo);
    }

    @Override
    @Transactional
    public ResultVo updateSubClass(SubClassVo subClassVo) {
        List<SubClassVo> list = subMapper.queryByName(subClassVo);
        if(!list.isEmpty()){
            for (SubClassVo s : list){
                if(s.getId() != subClassVo.getId()){
                    return ResultBuildVo.error("备件小类名称已存在，请重新输入！", "500");
                }
            }
        }
        subMapper.updateSubClass(subClassVo);
        return ResultBuildVo.success(subClassVo);
    }

    @Override
    @Transactional
    public ResultVo updateStatus(SubClassVo subClassVo) {
        subMapper.updateStatus(subClassVo);
        return ResultBuildVo.success(subClassVo);
    }

    @Override
    public ResultVo qureyPage(SubClassSelect subClassSelect) {
        int count = subMapper.countByCNS(subClassSelect);
        List<SubClass> subClasses = subMapper.queryLimit(subClassSelect);
        for (SubClass s : subClasses){
            if(s.getStatus() == 1){
                s.setStatusName(StatusEnum.getStatusName(s.getStatus()));
            }else{
                s.setStatusName(StatusEnum.getStatusName(s.getStatus()));
            }
        }
        AllRecords allRecords = new AllRecords();
        allRecords.setPageSize(subClassSelect.getPageSize());
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(subClasses);
        return ResultBuildVo.success(allRecords);
    }
}
